package com.rupeng.hibernate.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.rupeng.hibernate.cfg.Configuration;
import com.rupeng.hibernate.connection.ConnectionProvider;
import com.rupeng.hibernate.connection.ConnectionProviderImpl;

public class Hibernate {
	private DataBase db;

	private Properties properties;
	
	private ConnectionProvider connProvider;
	
	private Connection conn;
	
	public Hibernate(Configuration cfg) {
		this.properties = cfg.getCfgProperties();
		this.connProvider = new ConnectionProviderImpl();
		this.setConn(connProvider.getConnection(properties));
		this.db = new DataBaseImpl();
	}
	
	/**
	 * To save the PO
	 * @param obj PO to be saved
	 */
	public void save(Object obj) {
		String objName = obj.getClass().getName();
		String poXMLFileName = (String) properties.get(objName);
		Configuration.readPOMapping(poXMLFileName, properties);		
		
		String sql = db.createInsertSQL(properties, obj);
System.out.println("sql:" + sql);
		db.executeInsert(sql, conn);
	}

	/**
	 * load PO by specific id
	 * @param c
	 * @param id PO id
	 * @return PO
	 */
	public Object loadById(Class c, String id) {
		return null;
	}
	
	/**
	 * delete PO
	 * @param obj PO to be deleted
	 * @return true if delete PO successfully, or return false;
	 */
	public boolean delete(Object obj) {
		return false;
	}
	
	/**
	 * update PO
	 * @param obj PO to be updated
	 * @return true if update PO successfully, or return false;
	 */
	public boolean update(Object obj) {
		return false;
	}
	
	/**
	 * list a PO
	 * @param c
	 */
	public void list(Class c) {
		
	}
	
	/**
	 * close conn
	 * @return true if close conn successfully, or return false
	 */
	public boolean close() {		
		return connProvider.close(conn);
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}
}
